import { request, useUserStore, clearEmpty } from "jmash-core";
import qs from "qs";
import type { AxiosPromise } from "axios";
import type {
  DictTypeApi, DictTypeReq, DictTypeModel, DictTypePage, DictTypeList,
  DictTypeKey, DictTypeKeyList, DictTypeCreateReq, DictTypeUpdateReq,
  DictTypeMoveKey, DictTypeEnableKey,
  DictTypeExportReq, DictTypeImportReq
} from "./types";

class DictTypeApiImpl implements DictTypeApi {

  getKey(model: DictTypeModel): DictTypeKey {
    return { typeCode: model.typeCode } as DictTypeKey;
  }

  //查询翻页信息字典类型
  findPage(query?: DictTypeReq): AxiosPromise<DictTypePage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEntryType = (query.entryType != undefined);
      query.hasEnable = (query.enable != undefined);
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_type/page/" + tenant,
      method: "get",
      params: query,
    });
  }

  //查询列表信息字典类型
  findList(query?: DictTypeReq): AxiosPromise<DictTypeList> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEntryType = (query.entryType != undefined);
      query.hasEnable = (query.enable != undefined);
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_type/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  //查询字典类型
  findById(query: DictTypeKey): AxiosPromise<DictTypeModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_type/id/" + tenant + "/" + query.typeCode,
      method: "get",
    });
  }

  // 创建实体字典类型
  create(data: DictTypeCreateReq): AxiosPromise<DictTypeModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_type",
      method: "post",
      data: data,
    });
  }

  //修改实体字典类型
  update(data: DictTypeUpdateReq): AxiosPromise<DictTypeModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_type",
      method: "patch",
      data: data,
    });
  }

  //上移下移
  move(key: DictTypeKey, up: boolean): AxiosPromise<boolean> {
    let data: DictTypeMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_type/move",
      method: "put",
      data: data,
    });
  }

  //启用/禁用
  enable(key: DictTypeKey, enable: boolean): AxiosPromise<boolean> {
    let data: DictTypeEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_type/enable",
      method: "put",
      data: data,
    });
  }

  // 删除字典类型
  delete(query: DictTypeKey): AxiosPromise<DictTypeModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_type/id",
      method: "delete",
      params: query,
    });
  }

  // 批量删除字典类型
  batchDelete(keys: DictTypeKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.typeCode);
    const data = {
      tenant: useUserStore().user.tenant,
      typeCode: ids,
    } as DictTypeKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_type/batch",
      method: "put",
      data: data,
    });
  }

  //下载导入模板字典类型
  downloadTemplate(): AxiosPromise {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/dict/dict_type/" + tenant + "/template.xlsx",
      method: "get",
      responseType: "blob",
      params: {
        tenant: useUserStore().user.tenant,
      },
    });
  }

  // 导入字典类型
  importExcel(data: DictTypeImportReq): AxiosPromise<string> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_type/import",
      method: "patch",
      data: data,
    });
  }

  //导出字典类型Excel文件
  exportExcel(queryParams: DictTypeExportReq): AxiosPromise {
    const tenant = useUserStore().user.tenant;
    clearEmpty(queryParams);
    const query = qs.stringify(queryParams, {
      arrayFormat: "brackets",
      allowDots: true,
    });
    return request({
      url: "/v1/dict/dict_type/" + tenant + "/dict_types.xlsx?" + query,
      method: "get",
      responseType: "blob",
    });
  }

}

const dictTypeApi = new DictTypeApiImpl();
export { dictTypeApi };
